package com.myapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.myapplication.model.Home1;
import com.myapplication.model.Home4;
import com.myapplication.zhou.alldemo.R;
import com.myapplication.zhou.fragment.TopViewFragment;

import java.util.List;

/**
 * Created by zhou on 2016/6/21.
 */
public class TopRecycleViewAdapter extends RecyclerView.Adapter<TopRecycleViewAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<Home4> list;
    private List<Home1> home1List;
    private BitmapRequestBuilder<GlideUrl, Bitmap> requestBuilder;

    public TopRecycleViewAdapter(Context context, List<Home4> list, List<Home1> home1List) {
        this.context = context;
        this.list = list;
        this.home1List = home1List;
        inflater = LayoutInflater.from(context);
        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .error(R.mipmap.wutu)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    @Override
    public TopRecycleViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.top_recycle_view_adapter, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view, viewHolderClickListener);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(TopRecycleViewAdapter.MyViewHolder holder, int position) {



   /*     x.image().bind(holder.imageView2, list.get(position).getRectangle1_image());
        x.image().bind(holder.imageView3, list.get(position).getRectangle2_image());
        x.image().bind(holder.imageView4, list.get(position).getRectangle3_image());
        x.image().bind(holder.imageView5, list.get(position).getRectangle4_image());
        x.image().bind(holder.imageView6, list.get(position).getRectangle5_image());
        x.image().bind(holder.imageView7, list.get(position).getRectangle6_image());
        x.image().bind(holder.imageView8, list.get(position).getRectangle7_image());
        x.image().bind(holder.imageView9, list.get(position).getRectangle8_image());
        x.image().bind(holder.imageView1,home1List.get(position).getImage() );*/

        requestBuilder.load(new GlideUrl(home1List.get(position).getImage())).into(holder.imageView1);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle1_image())).into(holder.imageView2);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle2_image())).into(holder.imageView3);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle3_image())).into(holder.imageView4);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle4_image())).into(holder.imageView5);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle5_image())).into(holder.imageView6);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle6_image())).into(holder.imageView7);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle7_image())).into(holder.imageView8);
        requestBuilder.load(new GlideUrl(list.get(position).getRectangle8_image())).into(holder.imageView9);








        /*Glide.with(context).load(home1List.get(position).getImage()).
                error(R.mipmap.wutu).into(holder.imageView1);
        Glide.with(context).load(list.get(position).getRectangle1_image()).
                error(R.mipmap.wutu).into(holder.imageView2);
        Glide.with(context).load(list.get(position).getRectangle2_image()).
                error(R.mipmap.wutu).into(holder.imageView3);
        Glide.with(context).load(list.get(position).getRectangle3_image()).
                error(R.mipmap.wutu).into(holder.imageView4);
        Glide.with(context).load(list.get(position).getRectangle4_image()).
                error(R.mipmap.wutu).into(holder.imageView5);
        Glide.with(context).load(list.get(position).getRectangle5_image()).
                error(R.mipmap.wutu).into(holder.imageView6);
        Glide.with(context).load(list.get(position).getRectangle6_image()).
                error(R.mipmap.wutu).into(holder.imageView7);
        Glide.with(context).load(list.get(position).getRectangle7_image()).
                error(R.mipmap.wutu).into(holder.imageView8);
        Glide.with(context).load(list.get(position).getRectangle8_image()).
                error(R.mipmap.wutu).into(holder.imageView9);
*/
        holder.subTitle.setText(list.get(position).getTitle());
        if ("金钊源超市".equals(list.get(position).getTitle())) {
            holder.sub_line.setImageResource(R.color.colorAccent);
            holder.subTitle.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
        } else if ("中央厨房".equals(list.get(position).getTitle())) {
            holder.sub_line.setImageResource(R.color.color_red_accent);
            holder.subTitle.setBackgroundColor(context.getResources().getColor(R.color.color_red_accent));
        } else if ("土特产馆".equals(list.get(position).getTitle())) {
            holder.sub_line.setImageResource(R.color.color_blue_little);
            holder.subTitle.setBackgroundColor(context.getResources().getColor(R.color.color_blue_little));
        } else if ("网上药店".equals(list.get(position).getTitle())) {
            holder.sub_line.setImageResource(R.color.color_blue_mid);
            holder.subTitle.setBackgroundColor(context.getResources().getColor(R.color.color_blue_mid));
        } else {
            holder.sub_line.setImageResource(R.color.color_green_blue);
            holder.subTitle.setBackgroundColor(context.getResources().getColor(R.color.color_green_blue));
        }
        holder.bindView(list.get(position), home1List.get(position));
    }

    public void update(List<Home4> home4List) {
        this.list = home4List;
        notifyDataSetChanged();
    }

    viewHolderClickListener viewHolderClickListener = new viewHolderClickListener() {
        @Override
        public void OnClickListener(Object object, int postion, View view) {
            Home4 home4 = new Home4();
            Home1 home1 = new Home1();
            if (object instanceof Home4) {
                home4 = (Home4) object;
            } else
                home1 = (Home1) object;
            switch (view.getId()) {
                case R.id.layout1:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle1_type(), home4.getRectangle1_data());
                    Log.i("TTTTTAG","layout1");
                    break;
                case R.id.layout2:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle2_type(), home4.getRectangle2_data());
                    Log.i("TTTTTAG","layout2");
                    break;
                case R.id.layout3:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle3_type(), home4.getRectangle3_data());
                    Log.i("TTTTTAG","layout3");
                    break;
                case R.id.layout4:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle4_type(), home4.getRectangle4_data());
                    Log.i("TTTTTAG","layout4");
                    break;
                case R.id.layout5:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle5_type(), home4.getRectangle5_data());
                    Log.i("TTTTTAG","layout5");
                    break;
                case R.id.layout6:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle6_type(), home4.getRectangle6_data());
                    Log.i("TTTTTAG","layout6");
                    break;
                case R.id.layout7:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle7_type(), home4.getRectangle7_data());
                    Log.i("TTTTTAG","layout7");
                    break;
                case R.id.layout8:
                    new TopViewFragment().sentToOtherPage(context,home4.getRectangle8_type(), home4.getRectangle8_data());
                    Log.i("TTTTTAG","layout8");
                    break;
                case R.id.imageView1:
                    new TopViewFragment().sentToOtherPage(context,home1.getType(), home1.getData());
                    Log.i("TTTTTAG","imageview");
                    break;
            }
        }
    };


    @Override
    public int getItemCount() {
        return list.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView subTitle;
        private LinearLayout layout1, layout2, layout3, layout4, layout5, layout6, layout7, layout8;
        private ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6, imageView7, imageView8, imageView9;
        private ImageView sub_line;
        private viewHolderClickListener viewHolderClickListener;

        public MyViewHolder(View v, viewHolderClickListener viewHolderClickListener) {
            super(v);
            subTitle = (TextView) v.findViewById(R.id.sub_title);
            layout1 = (LinearLayout) v.findViewById(R.id.layout1);
            layout2 = (LinearLayout) v.findViewById(R.id.layout2);
            layout3 = (LinearLayout) v.findViewById(R.id.layout3);
            layout4 = (LinearLayout) v.findViewById(R.id.layout4);
            layout5 = (LinearLayout) v.findViewById(R.id.layout5);
            layout6 = (LinearLayout) v.findViewById(R.id.layout6);
            layout7 = (LinearLayout) v.findViewById(R.id.layout7);
            layout8 = (LinearLayout) v.findViewById(R.id.layout8);
            imageView1 = (ImageView) v.findViewById(R.id.imageView1);
            imageView2 = (ImageView) v.findViewById(R.id.imageView2);
            imageView3 = (ImageView) v.findViewById(R.id.imageView3);
            imageView4 = (ImageView) v.findViewById(R.id.imageView4);
            imageView5 = (ImageView) v.findViewById(R.id.imageView5);
            imageView6 = (ImageView) v.findViewById(R.id.imageView6);
            imageView7 = (ImageView) v.findViewById(R.id.imageView7);
            imageView8 = (ImageView) v.findViewById(R.id.imageView8);
            imageView9 = (ImageView) v.findViewById(R.id.imageView9);
            sub_line = (ImageView) v.findViewById(R.id.sub_line1);
            this.viewHolderClickListener = viewHolderClickListener;
            layout1.setOnClickListener(this);
            layout2.setOnClickListener(this);
            layout3.setOnClickListener(this);
            layout4.setOnClickListener(this);
            layout5.setOnClickListener(this);
            layout6.setOnClickListener(this);
            layout7.setOnClickListener(this);
            layout8.setOnClickListener(this);
            imageView1.setOnClickListener(this);

        }

        public void bindView(Home4 home4, Home1 home1) {
            layout1.setTag(R.id.imageView, home4);
            layout2.setTag(R.id.imageView, home4);
            layout3.setTag(R.id.imageView, home4);
            layout4.setTag(R.id.imageView, home4);
            layout5.setTag(R.id.imageView, home4);
            layout6.setTag(R.id.imageView, home4);
            layout7.setTag(R.id.imageView, home4);
            layout8.setTag(R.id.imageView, home4);
            imageView1.setTag(R.id.imageView, home1);

        }

        @Override
        public void onClick(View v) {
            viewHolderClickListener.OnClickListener(v.getTag(R.id.imageView), getLayoutPosition(), v);
        }
    }

    public interface viewHolderClickListener {

        void OnClickListener(Object object, int position, View view);
    }
}
